package com.briup.smartcity.service.impl;

import com.briup.smartcity.bean.BaseUser;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.BaseUserMapper;
import com.briup.smartcity.mapper.extend.BaseUserExtendMapper;
import com.briup.smartcity.service.IBaseUserService;
import com.briup.smartcity.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/*service层实现类*/
@Service
public class IBaseUserServiceImpl implements IBaseUserService {
    @Autowired
    private BaseUserMapper mapper;//实现简单查询


    @Autowired
    private BaseUserExtendMapper extendMapper;//实现复杂查询


    @Override
    public String login(String username, String password) throws ServiceException {
        //根据用户名查询用户
        BaseUser baseUser = extendMapper.selectByUsername(username);
        //用户存在
        if (baseUser != null) {
            //登陆成功
            if (password.equals(baseUser.getPassword())) {
                //生成token
                System.out.println("登陆成功");
                HashMap<String, Object> map = new HashMap<>();
                final String token = JwtUtil.sign(username, map);
                /*JwtBuilder builder = Jwts.builder();

                String token = builder.setSubject(username)  //token中携带的数据
                        .setIssuedAt(new Date())      //token的生成时间
                        .setId(baseUser.getUserId()+"")//设置用户id为token id
                        .setExpiration(new Date(System.currentTimeMillis()+60*60*24*1000)) //设置过期时间为1天
                        .signWith(SignatureAlgorithm.HS256,"xiaodu")//设置加密方式和密码
                        .compact();*/
                return token;
            }
            //密码错误
            else {

                throw new ServiceException("密码错误");
            }
        }
        //用户不存在
        else {
            /*System.out.println("用户不存在");
            return new Result(0,"用户不存在",null);*/
            throw new ServiceException("用户不存在");
        }
    }

    @Override
    public BaseUser currentUserInfo(String token) throws ServiceException {
        System.out.println("token:" + token);
        //1.解析token字符串 获取当前用户的用户名
        String username = JwtUtil.getUserId(token);
        //2.调用mapper查询根据username获取用户的所有信息
        return extendMapper.selectByUsername(username);
    }

    @Override
    public PageInfo<BaseUser> pageQuery(int pageNum, int pageSize, String username, Integer role) throws ServiceException {
        //1.设置分页信息
        PageHelper.startPage(pageNum, pageSize, true);

        //2.查询所有轮播图信息
        List<BaseUser> list = extendMapper.pageQueryWithBaseUser(username, role);

        PageInfo<BaseUser> pageInfo = new PageInfo<>(list);

        //3.将list对象封装到PageInfo对象当中
        return pageInfo;
    }

    public void updateUser(String token,BaseUser baseUser) throws ServiceException {

        String username = JwtUtil.getUserId(token);
        BaseUser baseUser4 = extendMapper.selectByUsername(username);
        if (baseUser4.getRole() == 0){
            mapper.updateByPrimaryKeySelective(baseUser);//传值
        }else{
            if (baseUser.getUserId() == baseUser4.getUserId()){
                mapper.updateByPrimaryKeySelective(baseUser);
            }else {
                throw new ServiceException("该用户不是管理员");
            }
        }

    }

    @Override
    public void updateUserIcon(String token,int id, String alterUserIcon) throws ServiceException {

        BaseUser user = new BaseUser();//创建一个新的对象去保存获取到的信息，再将对象传入到对应的接口中
        user.setUserId(id);
        user.setIcon(alterUserIcon);
        String username = JwtUtil.getUserId(token);
        BaseUser baseUser3 = extendMapper.selectByUsername(username);
        if (baseUser3.getRole() == 0){
            mapper.updateByPrimaryKeySelective(user);
        }else{
            if (id == baseUser3.getUserId()){
                mapper.updateByPrimaryKeySelective(user);
            }else {
                throw new ServiceException("该用户不是管理员");
            }
        }

    }

    @Override
    public void deleteUserById(String token,Integer id) throws ServiceException {
        BaseUser baseUser = mapper.selectByPrimaryKey(id);
        String username = JwtUtil.getUserId(token);
        BaseUser baseUser2 = extendMapper.selectByUsername(username);
        if (baseUser2.getRole() == 0) {
            mapper.deleteByPrimaryKey(id);
        } else {
         throw new ServiceException("该用户不是管理员");
       }


    }

    @Override
    public void insertUser(BaseUser baseUser) throws ServiceException {
       // if (baseUser.getRole() == 0 && baseUser.getUsername() != null && baseUser.getPassword() != null && baseUser.getUserId() != null && baseUser.getRealname() != null) {
            mapper.insertSelective(baseUser);
      //  } else if (baseUser.getUsername() == null && baseUser.getPassword() == null && baseUser.getUserId() == null && baseUser.getRealname() == null) {
            //System.out.println("用户不存在");
            // return new Result(0,"用户不存在",null);
          //  throw new ServiceException("请把用户名、密码、真实姓名和userid填完整");
       // } else {
          //  throw new ServiceException("该用户不是管理员");
       // }
    }
}
